package com.firstresponder.activity;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import android.graphics.Color;
import android.graphics.PorterDuff;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyExpandableAdminListAdapter extends BaseExpandableListAdapter {
	// Sample data set. children[i] contains the children (String[]) for
	// groups[i].
	private static String TAG = "MyExpandableAdminListAdapter.java";
	private String[] groups = { "     GPS", "     PASS", "     Bluetooth" };
	private String[][] children = {
			{ " " },
			{ " " },
			{ " " }, { "Child4" }, { "Child5" } };
//	private String[][] children = {
//			{ "GPS Tracking: ", " ", "GPS Accuracy: ", " ",
//					"GPS Daily Updates: ", " " },
//			{ "Status: ", " ", "Sensitivity: ", " ", 
//						"Alert Delay: ", " " },
//			{ "Status: ", " ", "Sensitivity: ", " ", "Bluetooth Address: ",
//					" ", " " }, { "Child4" }, { "Child5" } };
	private static FirstResponder MAIN_ACTIVITY = null;
	private static String MAC_ADDRESS;
	private int grayishColor = 0xFFDDE1DD;
	private int greenishColor = 0xFF0F9521;
	// buttons that have to be dynamically places inside the expandable list
	// view
	private Button gpsTrackingOn, gpsTrackingOff, gpsAccuracyStreet,
			gpsAccuracyCity, gpsDailyUpdates6x, gpsDailyUpdates12x,
			gpsDailyUpdates24x, statusOn, statusOff, sensitivityLow,
			sensitivityMedium, sensitivityHigh, alertDelay1min, alertDelay2min,
			alertDelay3min, allowAccessYes, allowAccessNo, bSensitivityLow,
			bSensitivityMedium, bSensitivityHigh, sendAddressNow;
	// initialize and set default settings for the user.
	private String gpsTracking;
	private String gpsAccuracy;
	private String gpsDailyUpdates;
	private String status;
	private String sensitivity;
	private String alertDelay ;
	private String allowAccess;
	private String bSensitivity ;
	private TextView bluetoothAddress;
	private String charset = "UTF-8";
	private ArrayList<String> prefs=new ArrayList<String>();
	private int count=0;

	public Object getChild(int groupPosition, int childPosition) {
		return children[groupPosition][childPosition];

	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public int getChildrenCount(int groupPosition) {
		int i = 0;
		try {
			i = children[groupPosition].length;

		} catch (Exception e) {
		}

		return i;
	}

	public TextView getGenericTextView() {
		// Layout parameters for the ExpandableListView
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT, 64);

		TextView textView = new TextView(MAIN_ACTIVITY);
		textView.setLayoutParams(lp);
		// Center the text vertically
		textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		textView.setTextColor(grayishColor);
		// Set the text starting position
		textView.setPadding(36, 0, 0, 0);
		return textView;
	}

	// test to add dynamic thigns to the child
	public LinearLayout getGenericLinearLayoutView() {
		// Layout parameters for the ExpandableListView
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

		LinearLayout linearLayout = new LinearLayout(MAIN_ACTIVITY);
		linearLayout.setLayoutParams(lp);

		// Set the text starting position
		linearLayout.setPadding(36, 0, 0, 0);
		return linearLayout;
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
Log.i(TAG, "getCHildView() Called!");
		if (groupPosition == 0) {
			// MyExpandableListAdapter listeners
				if(count==0)
				{
					Log.i(TAG,"Loading preferences...");
					getPreferences();
					count++;
				}
				LinearLayout gpsLayout=getGenericLinearLayoutView();
				gpsLayout.setOrientation(1);
				TextView gpsTrackingText= getGenericTextView();
				gpsTrackingText.setText("GPS Tracking: ");	
				gpsTrackingOn = new Button(MAIN_ACTIVITY);
				gpsTrackingOff = new Button(MAIN_ACTIVITY);
						
				 gpsTrackingOn.setOnClickListener(new OnClickListener() {
				 @Override
				 public void onClick(View v) {
					 gpsTracking="on";
					 savePreferences();
					 getPreferences();
					 
				 gpsTrackingOn.getBackground().setColorFilter(0xFF+greenishColor,
							PorterDuff.Mode.MULTIPLY);
				 gpsTrackingOff.getBackground().setColorFilter(0xFF+grayishColor,
							PorterDuff.Mode.MULTIPLY);	
				 }
				 });
				 gpsTrackingOff.setOnClickListener(new OnClickListener() {
					 @Override
					 public void onClick(View v) {
						 gpsTracking="off";
						 savePreferences();
						 getPreferences();		
						 gpsTrackingOn.getBackground().setColorFilter(0xFF+grayishColor,
									PorterDuff.Mode.MULTIPLY);	
						 gpsTrackingOff.getBackground().setColorFilter(0xFF+greenishColor,
									PorterDuff.Mode.MULTIPLY);	
					 }
					 });
				 if(gpsTracking.equalsIgnoreCase("on"))
				 {
					 gpsTrackingOn.getBackground().setColorFilter(0xFF+greenishColor,
								PorterDuff.Mode.MULTIPLY);	
				 }
				 else if(gpsTracking.equalsIgnoreCase("off"))
				 {
					 gpsTrackingOff.getBackground().setColorFilter(0xFF+greenishColor,
								PorterDuff.Mode.MULTIPLY);	
				 }
				
				gpsTrackingOn.setText(" On ");
				gpsTrackingOff.setText(" Off ");
				LinearLayout linearlayout1 = getGenericLinearLayoutView();
				linearlayout1.setOrientation(0);
				linearlayout1.addView(gpsTrackingOn);
				linearlayout1.addView(gpsTrackingOff);
				gpsLayout.addView(gpsTrackingText);
				gpsLayout.addView(linearlayout1);
				
				TextView gpsAccuracyText= getGenericTextView();
				gpsAccuracyText.setText("GPS Accuracy: ");
				gpsAccuracyStreet = new Button(MAIN_ACTIVITY);
				gpsAccuracyCity = new Button(MAIN_ACTIVITY);
				gpsAccuracyStreet.setOnClickListener(new OnClickListener() {
					 @Override
					 public void onClick(View v) {
						 gpsAccuracy="street";
						 savePreferences();
						 getPreferences();
						 
						 gpsAccuracyStreet.getBackground().setColorFilter(0xFF+greenishColor,
								PorterDuff.Mode.MULTIPLY);
						 gpsAccuracyCity.getBackground().setColorFilter(0xFF+grayishColor,
								PorterDuff.Mode.MULTIPLY);	
					 }
					 });
				gpsAccuracyCity.setOnClickListener(new OnClickListener() {
						 @Override
						 public void onClick(View v) {
							 gpsAccuracy="city";
							 savePreferences();
							 getPreferences();		
							 gpsAccuracyStreet.getBackground().setColorFilter(0xFF+grayishColor,
										PorterDuff.Mode.MULTIPLY);	
							 gpsAccuracyCity.getBackground().setColorFilter(0xFF+greenishColor,
										PorterDuff.Mode.MULTIPLY);	
						 }
						 });
					 if(gpsAccuracy.equalsIgnoreCase("street"))
					 {
						 gpsAccuracyStreet.getBackground().setColorFilter(0xFF+greenishColor,
									PorterDuff.Mode.MULTIPLY);	
					 }
					 else if(gpsAccuracy.equalsIgnoreCase("city"))
					 {
						 gpsAccuracyCity.getBackground().setColorFilter(0xFF+greenishColor,
									PorterDuff.Mode.MULTIPLY);	
					 }

				gpsAccuracyStreet.setText(" Street ");
				gpsAccuracyCity.setText(" City ");
				LinearLayout linearlayout2 = getGenericLinearLayoutView();
				linearlayout2.setOrientation(0);
				linearlayout2.addView(gpsAccuracyStreet);
				linearlayout2.addView(gpsAccuracyCity);
				gpsLayout.addView(gpsAccuracyText);
				gpsLayout.addView(linearlayout2);		
				
				TextView gpsDailyUpdatesText= getGenericTextView();
				gpsDailyUpdatesText.setText("GPS Daily Updates: ");
				
				gpsDailyUpdates6x = new Button(MAIN_ACTIVITY);
				gpsDailyUpdates12x = new Button(MAIN_ACTIVITY);
				gpsDailyUpdates24x = new Button(MAIN_ACTIVITY);
				
				
				gpsDailyUpdates6x.setOnClickListener(new OnClickListener() {
				 @Override
				 public void onClick(View v) {
					 gpsDailyUpdates="6x";
					 savePreferences();
					 getPreferences();
					 
					 gpsDailyUpdates6x.getBackground().setColorFilter(0xFF+greenishColor,
							PorterDuff.Mode.MULTIPLY);
					 gpsDailyUpdates12x.getBackground().setColorFilter(0xFF+grayishColor,
							PorterDuff.Mode.MULTIPLY);	
					 gpsDailyUpdates24x.getBackground().setColorFilter(0xFF+grayishColor,
							PorterDuff.Mode.MULTIPLY);	
				 }
				 });
				gpsDailyUpdates12x.setOnClickListener(new OnClickListener() {
					 @Override
					 public void onClick(View v) {
						 gpsDailyUpdates="12x";
						 savePreferences();
						 getPreferences();		
						 gpsDailyUpdates6x.getBackground().setColorFilter(0xFF+grayishColor,
									PorterDuff.Mode.MULTIPLY);	
						 gpsDailyUpdates12x.getBackground().setColorFilter(0xFF+greenishColor,
									PorterDuff.Mode.MULTIPLY);	
						 gpsDailyUpdates24x.getBackground().setColorFilter(0xFF+grayishColor,
									PorterDuff.Mode.MULTIPLY);	
					 }
					 });
				 gpsDailyUpdates24x.setOnClickListener(new OnClickListener() {
					 @Override
					 public void onClick(View v) {
						 gpsDailyUpdates="24x";
						 savePreferences();
						 getPreferences();		
						 gpsDailyUpdates6x.getBackground().setColorFilter(0xFF+grayishColor,
									PorterDuff.Mode.MULTIPLY);	
						 gpsDailyUpdates24x.getBackground().setColorFilter(0xFF+greenishColor,
									PorterDuff.Mode.MULTIPLY);	
						 gpsDailyUpdates12x.getBackground().setColorFilter(0xFF+grayishColor,
									PorterDuff.Mode.MULTIPLY);	
					 }
					 });
				 if(gpsDailyUpdates.equalsIgnoreCase("6x"))
				 {
					 gpsDailyUpdates6x.getBackground().setColorFilter(0xFF+greenishColor,
								PorterDuff.Mode.MULTIPLY);	
				 }
				 else if(gpsDailyUpdates.equalsIgnoreCase("12x"))
				 {
					 gpsDailyUpdates12x.getBackground().setColorFilter(0xFF+greenishColor,
								PorterDuff.Mode.MULTIPLY);	
				 }

				 else if(gpsDailyUpdates.equalsIgnoreCase("24x"))
				 {
					 gpsDailyUpdates24x.getBackground().setColorFilter(0xFF+greenishColor,
								PorterDuff.Mode.MULTIPLY);	
				 }
				gpsDailyUpdates6x.setText(" 6x ");
				gpsDailyUpdates12x.setText(" 12x ");
				gpsDailyUpdates24x.setText(" 24x ");
				LinearLayout linearlayout3 = getGenericLinearLayoutView();
				linearlayout3.setOrientation(0);
				linearlayout3.addView(gpsDailyUpdates6x);
				linearlayout3.addView(gpsDailyUpdates12x);
				linearlayout3.addView(gpsDailyUpdates24x);
				gpsLayout.addView(gpsDailyUpdatesText);
				gpsLayout.addView(linearlayout3);
                
				return gpsLayout; 			
		}
		if (groupPosition == 1) {
				if(count==0)
				{
					Log.i(TAG,"Loading preferences...");
					getPreferences();
					count++;
				}
				LinearLayout passLayout=getGenericLinearLayoutView();
				passLayout.setOrientation(1);
				
				TextView passStatusText= getGenericTextView();
				passStatusText.setText("Status: ");
		
				statusOn = new Button(MAIN_ACTIVITY);
				statusOff = new Button(MAIN_ACTIVITY);
				//****************************************************
				statusOn.setOnClickListener(new OnClickListener() {
					 @Override
					 public void onClick(View v) {
						 status="on";
						 savePreferences();
						 getPreferences();			 
						 statusOn.getBackground().setColorFilter(0xFF+greenishColor,
								PorterDuff.Mode.MULTIPLY);
					 statusOff.getBackground().setColorFilter(0xFF+grayishColor,
								PorterDuff.Mode.MULTIPLY);	
					 }
					 });
					 statusOff.setOnClickListener(new OnClickListener() {
						 @Override
						 public void onClick(View v) {
							 status="off";
							 savePreferences();
							 getPreferences();		
							 statusOn.getBackground().setColorFilter(0xFF+grayishColor,
										PorterDuff.Mode.MULTIPLY);	
							 statusOff.getBackground().setColorFilter(0xFF+greenishColor,
										PorterDuff.Mode.MULTIPLY);	
						 }
						 });
					 if(status.equalsIgnoreCase("on"))
					 {
						 statusOn.getBackground().setColorFilter(0xFF+greenishColor,
									PorterDuff.Mode.MULTIPLY);	
					 }
					 else if(status.equalsIgnoreCase("off"))
					 {
						 statusOff.getBackground().setColorFilter(0xFF+greenishColor,
									PorterDuff.Mode.MULTIPLY);	
					 }
					//****************************************************
				statusOn.setText(" On ");
				statusOff.setText(" Off ");
				LinearLayout linearlayout4 = getGenericLinearLayoutView();
				linearlayout4.setOrientation(0);
				linearlayout4.addView(statusOn);
				linearlayout4.addView(statusOff);
				passLayout.addView(passStatusText);
				passLayout.addView(linearlayout4);
		
				TextView passSensitivityText= getGenericTextView();
				passSensitivityText.setText("Sensitivity: ");
			
				sensitivityLow = new Button(MAIN_ACTIVITY);
				sensitivityMedium = new Button(MAIN_ACTIVITY);
				sensitivityHigh = new Button(MAIN_ACTIVITY);
		
				//************************
				sensitivityLow.setOnClickListener(new OnClickListener() {
					 @Override
					 public void onClick(View v) {
						 sensitivity="low";
						 savePreferences();
						 getPreferences();
						 
						 sensitivityLow.getBackground().setColorFilter(0xFF+greenishColor,
								PorterDuff.Mode.MULTIPLY);
						 sensitivityMedium.getBackground().setColorFilter(0xFF+grayishColor,
								PorterDuff.Mode.MULTIPLY);	
						 sensitivityHigh.getBackground().setColorFilter(0xFF+grayishColor,
								PorterDuff.Mode.MULTIPLY);	
					 }
					 });
				sensitivityMedium.setOnClickListener(new OnClickListener() {
						 @Override
						 public void onClick(View v) {
							 sensitivity="medium";
							 savePreferences();
							 getPreferences();		
							 sensitivityLow.getBackground().setColorFilter(0xFF+grayishColor,
										PorterDuff.Mode.MULTIPLY);	
							 sensitivityMedium.getBackground().setColorFilter(0xFF+greenishColor,
										PorterDuff.Mode.MULTIPLY);	
							 sensitivityHigh.getBackground().setColorFilter(0xFF+grayishColor,
										PorterDuff.Mode.MULTIPLY);	
						 }
						 });
				sensitivityHigh.setOnClickListener(new OnClickListener() {
						 @Override
						 public void onClick(View v) {
							 sensitivity="high";
							 savePreferences();
							 getPreferences();		
							 sensitivityLow.getBackground().setColorFilter(0xFF+grayishColor,
										PorterDuff.Mode.MULTIPLY);	
							 sensitivityHigh.getBackground().setColorFilter(0xFF+greenishColor,
										PorterDuff.Mode.MULTIPLY);	
							 sensitivityMedium.getBackground().setColorFilter(0xFF+grayishColor,
										PorterDuff.Mode.MULTIPLY);	
						 }
						 });
					 if(sensitivity.equalsIgnoreCase("low"))
					 {
						 sensitivityLow.getBackground().setColorFilter(0xFF+greenishColor,
									PorterDuff.Mode.MULTIPLY);	
					 }
					 else if(sensitivity.equalsIgnoreCase("medium"))
					 {
						 sensitivityMedium.getBackground().setColorFilter(0xFF+greenishColor,
									PorterDuff.Mode.MULTIPLY);	
					 }

					 else if(sensitivity.equalsIgnoreCase("high"))
					 {
						 sensitivityHigh.getBackground().setColorFilter(0xFF+greenishColor,
									PorterDuff.Mode.MULTIPLY);	
					 }
				//************************
				sensitivityLow.setText(" Low ");
				sensitivityMedium.setText(" Medium ");
				sensitivityHigh.setText(" High ");

				LinearLayout linearlayout5 = getGenericLinearLayoutView();
				linearlayout5.setOrientation(0);
				linearlayout5.addView(sensitivityLow);
				linearlayout5.addView(sensitivityMedium);
				linearlayout5.addView(sensitivityHigh);
				passLayout.addView(passSensitivityText);
				passLayout.addView(linearlayout5);

			
				TextView passAlertDelayText= getGenericTextView();
				passAlertDelayText.setText("Alert Delay: ");
				
				alertDelay1min = new Button(MAIN_ACTIVITY);
				alertDelay2min = new Button(MAIN_ACTIVITY);
				alertDelay3min = new Button(MAIN_ACTIVITY);
				
				//************************
				alertDelay1min.setOnClickListener(new OnClickListener() {
					 @Override
					 public void onClick(View v) {
						 alertDelay="1min";
						 savePreferences();
						 getPreferences();
						 
						 alertDelay1min.getBackground().setColorFilter(0xFF+greenishColor,
								PorterDuff.Mode.MULTIPLY);
						 alertDelay2min.getBackground().setColorFilter(0xFF+grayishColor,
								PorterDuff.Mode.MULTIPLY);	
						 alertDelay3min.getBackground().setColorFilter(0xFF+grayishColor,
								PorterDuff.Mode.MULTIPLY);	
					 }
					 });
				alertDelay2min.setOnClickListener(new OnClickListener() {
						 @Override
						 public void onClick(View v) {
							 alertDelay="2min";
							 savePreferences();
							 getPreferences();		
							 alertDelay1min.getBackground().setColorFilter(0xFF+grayishColor,
										PorterDuff.Mode.MULTIPLY);	
							 alertDelay2min.getBackground().setColorFilter(0xFF+greenishColor,
										PorterDuff.Mode.MULTIPLY);	
							 alertDelay3min.getBackground().setColorFilter(0xFF+grayishColor,
										PorterDuff.Mode.MULTIPLY);	
						 }
						 });
				alertDelay3min.setOnClickListener(new OnClickListener() {
						 @Override
						 public void onClick(View v) {
							 alertDelay="3min";
							 savePreferences();
							 getPreferences();		
							 alertDelay1min.getBackground().setColorFilter(0xFF+grayishColor,
										PorterDuff.Mode.MULTIPLY);	
							 alertDelay3min.getBackground().setColorFilter(0xFF+greenishColor,
										PorterDuff.Mode.MULTIPLY);	
							 alertDelay2min.getBackground().setColorFilter(0xFF+grayishColor,
										PorterDuff.Mode.MULTIPLY);	
						 }
						 });
					 if(alertDelay.equalsIgnoreCase("1min"))
					 {
						 alertDelay1min.getBackground().setColorFilter(0xFF+greenishColor,
									PorterDuff.Mode.MULTIPLY);	
					 }
					 else if(alertDelay.equalsIgnoreCase("2min"))
					 {
						 alertDelay2min.getBackground().setColorFilter(0xFF+greenishColor,
									PorterDuff.Mode.MULTIPLY);	
					 }

					 else if(alertDelay.equalsIgnoreCase("3min"))
					 {
						 alertDelay3min.getBackground().setColorFilter(0xFF+greenishColor,
									PorterDuff.Mode.MULTIPLY);	
					 }
				//************************

				alertDelay1min.setText(" 1 min ");
				alertDelay2min.setText(" 2 min ");
				alertDelay3min.setText(" 3 min ");
				LinearLayout linearlayout6 = getGenericLinearLayoutView();
				linearlayout6.setOrientation(0);
				linearlayout6.addView(alertDelay1min);
				linearlayout6.addView(alertDelay2min);
				linearlayout6.addView(alertDelay3min);
				passLayout.addView(passAlertDelayText);
				passLayout.addView(linearlayout6);
				
		        return passLayout;
		}
		else if(groupPosition==2){
				if(count==0)
				{
					Log.i(TAG,"Loading preferences...");
					getPreferences();
					count++;
				}
				LinearLayout bluetoothLayout=getGenericLinearLayoutView();
				bluetoothLayout.setOrientation(1);
				TextView bluetoothStatusText= getGenericTextView();
				bluetoothStatusText.setText("Status: ");
				allowAccessYes = new Button(MAIN_ACTIVITY);
				allowAccessNo = new Button(MAIN_ACTIVITY);
				
				//****************************************************
				allowAccessYes.setOnClickListener(new OnClickListener() {
					 @Override
					 public void onClick(View v) {
						 allowAccess="yes";
						 savePreferences();
						 getPreferences();
						 
						 allowAccessYes.getBackground().setColorFilter(0xFF+greenishColor,
								PorterDuff.Mode.MULTIPLY);
						 allowAccessNo.getBackground().setColorFilter(0xFF+grayishColor,
								PorterDuff.Mode.MULTIPLY);	
					 }
					 });
				allowAccessNo.setOnClickListener(new OnClickListener() {
						 @Override
						 public void onClick(View v) {
							 allowAccess="no";
							 savePreferences();
							 getPreferences();		
							 allowAccessYes.getBackground().setColorFilter(0xFF+grayishColor,
										PorterDuff.Mode.MULTIPLY);	
							 allowAccessNo.getBackground().setColorFilter(0xFF+greenishColor,
										PorterDuff.Mode.MULTIPLY);	
						 }
						 });
					 if(allowAccess.equalsIgnoreCase("yes"))
					 {
						 allowAccessYes.getBackground().setColorFilter(0xFF+greenishColor,
									PorterDuff.Mode.MULTIPLY);	
					 }
					 else if(allowAccess.equalsIgnoreCase("no"))
					 {
						 allowAccessNo.getBackground().setColorFilter(0xFF+greenishColor,
									PorterDuff.Mode.MULTIPLY);	
					 }
					//****************************************************
				allowAccessYes.setText(" Yes ");
				allowAccessNo.setText(" No ");
				LinearLayout linearlayout7 = getGenericLinearLayoutView();
				linearlayout7.setOrientation(0);
				linearlayout7.addView(allowAccessYes);
				linearlayout7.addView(allowAccessNo);
				bluetoothLayout.addView(bluetoothStatusText);
				bluetoothLayout.addView(linearlayout7);
			
				TextView bluetoothSensitivityText= getGenericTextView();
				bluetoothSensitivityText.setText("Sensitivity: ");
				
				bSensitivityLow = new Button(MAIN_ACTIVITY);
				bSensitivityMedium = new Button(MAIN_ACTIVITY);
				bSensitivityHigh = new Button(MAIN_ACTIVITY);
				
				//*****************************************
				bSensitivityLow.setOnClickListener(new OnClickListener() {
					 @Override
					 public void onClick(View v) {
						 bSensitivity="low";
						 savePreferences();
						 getPreferences();
						 
						 bSensitivityLow.getBackground().setColorFilter(0xFF+greenishColor,
								PorterDuff.Mode.MULTIPLY);
						 bSensitivityMedium.getBackground().setColorFilter(0xFF+grayishColor,
								PorterDuff.Mode.MULTIPLY);	
						 bSensitivityHigh.getBackground().setColorFilter(0xFF+grayishColor,
								PorterDuff.Mode.MULTIPLY);	
					 }
					 });
				bSensitivityMedium.setOnClickListener(new OnClickListener() {
						 @Override
						 public void onClick(View v) {
							 bSensitivity="medium";
							 savePreferences();
							 getPreferences();		
							 bSensitivityLow.getBackground().setColorFilter(0xFF+grayishColor,
										PorterDuff.Mode.MULTIPLY);	
							 bSensitivityMedium.getBackground().setColorFilter(0xFF+greenishColor,
										PorterDuff.Mode.MULTIPLY);	
							 bSensitivityHigh.getBackground().setColorFilter(0xFF+grayishColor,
										PorterDuff.Mode.MULTIPLY);	
						 }
						 });
				bSensitivityHigh.setOnClickListener(new OnClickListener() {
						 @Override
						 public void onClick(View v) {
							 bSensitivity="high";
							 savePreferences();
							 getPreferences();		
							 bSensitivityLow.getBackground().setColorFilter(0xFF+grayishColor,
										PorterDuff.Mode.MULTIPLY);	
							 bSensitivityHigh.getBackground().setColorFilter(0xFF+greenishColor,
										PorterDuff.Mode.MULTIPLY);	
							 bSensitivityMedium.getBackground().setColorFilter(0xFF+grayishColor,
										PorterDuff.Mode.MULTIPLY);	
						 }
						 });
					 if(bSensitivity.equalsIgnoreCase("low"))
					 {
						 bSensitivityLow.getBackground().setColorFilter(0xFF+greenishColor,
									PorterDuff.Mode.MULTIPLY);	
					 }
					 else if(bSensitivity.equalsIgnoreCase("medium"))
					 {
						 bSensitivityMedium.getBackground().setColorFilter(0xFF+greenishColor,
									PorterDuff.Mode.MULTIPLY);	
					 }

					 else if(bSensitivity.equalsIgnoreCase("high"))
					 {
						 bSensitivityHigh.getBackground().setColorFilter(0xFF+greenishColor,
									PorterDuff.Mode.MULTIPLY);	
					 }
				//*****************************************

				bSensitivityLow.setText(" Low ");
				bSensitivityMedium.setText(" Medium ");
				bSensitivityHigh.setText(" High ");

				LinearLayout linearlayout8 = getGenericLinearLayoutView();
				linearlayout8.setOrientation(0);
				linearlayout8.addView(bSensitivityLow);
				linearlayout8.addView(bSensitivityMedium);
				linearlayout8.addView(bSensitivityHigh);
				bluetoothLayout.addView(bluetoothSensitivityText);
				bluetoothLayout.addView(linearlayout8);
			    
				TextView bluetoothAddressText= getGenericTextView();
				bluetoothAddressText.setText("Bluetooth Address: ");
				bluetoothAddress = new TextView(MAIN_ACTIVITY);
				bluetoothAddress.setTextColor(0xFF1DC5E6);
				bluetoothAddress.setText(MAC_ADDRESS);
				LinearLayout linearlayout9 = getGenericLinearLayoutView();
				linearlayout9.setOrientation(1);
				linearlayout9.addView(bluetoothAddress);
				bluetoothLayout.addView(bluetoothAddressText);
				bluetoothLayout.addView(linearlayout9);
				

			
				sendAddressNow = new Button(MAIN_ACTIVITY);
				sendAddressNow.setText(" SEND ADDRESS NOW ");
				AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
						ViewGroup.LayoutParams.WRAP_CONTENT, 64);

				LinearLayout linearlayout10 = new LinearLayout(MAIN_ACTIVITY);
				linearlayout10.setLayoutParams(lp);

				// Set the text starting position
				linearlayout10.setPadding(36, 0, 0, 0);
				linearlayout10.setOrientation(1);
				linearlayout10.addView(sendAddressNow);
				bluetoothLayout.addView(linearlayout10);
				return bluetoothLayout;

			
		}

		return null;

	}

	public Object getGroup(int groupPosition) {
		return groups[groupPosition];
	}

	public int getGroupCount() {
		return groups.length;
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		
		TextView textView = getGenericTextView();
		textView.setText(getGroup(groupPosition).toString());
		return textView;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public boolean hasStableIds() {
		return true;
	}

	public static void setMainActivity(FirstResponder activity) {
		MAIN_ACTIVITY = activity;
	}

	public static void setMacAddress(String mac) {
		MAC_ADDRESS = mac;
	}

	public void savePreferences()
	{
			
			//File file = new File("/sdcard/download/preferences.txt");
			File file = MAIN_ACTIVITY.getFileStreamPath("preferences.txt");
			if(file.exists())
			{
				try{
					File temp = File.createTempFile("file", ".txt", file.getParentFile());
					//BufferedWriter used to cancel out the performance penalty because this will be called a alot.
					
					BufferedReader reader = new BufferedReader(new FileReader(file));
					//FileWriter pref = new FileWriter("/sdcard/download/preferences.txt");	
					BufferedWriter writer = new BufferedWriter(new FileWriter(temp));
		              
		                  
		               // FileOutputStream fOut = MAIN_ACTIVITY.openFileOutput("preferences.txt",
		               // 		MAIN_ACTIVITY.MODE_PRIVATE);
		                
		               // PrintWriter writer = new PrintWriter(new OutputStreamWriter(fOut)); 
		                //Read the file line by line. and erase
		                for (String line; (line = reader.readLine()) != null;) {
		                	line = " ";               	
		                }
		                // Write the strings to the file/ save the preferences
		                writer.write(gpsTracking+",");
		                writer.write(gpsAccuracy+",");
		                writer.write(gpsDailyUpdates+",");
		                writer.write(status+",");
		                writer.write(sensitivity+",");
		            	writer.write(alertDelay+",");
		            	writer.write(allowAccess+",");
		            	writer.write(bSensitivity+",");
		                /* ensure that everything is
		                 * really written out and close */
		                writer.flush();

		                
		                reader.close();
		                writer.close();
		                
		                file.delete();
		                temp.renameTo(file);
			
			}
			 catch (IOException ioe) {
				Log.e(TAG,"ERROR trying to RE-access file");
			        ioe.printStackTrace();
			}
			}
             else{
            	 try{
            		 
            	 
            	 File newFile = new File("/data/data/com.firstresponder.activity/files/preferences.txt");
            	 BufferedWriter writer = new BufferedWriter(new FileWriter(newFile));
            	 // Write the strings to the file/ save the preferences
	                writer.write(gpsTracking+",");
	                writer.write(gpsAccuracy+",");
	                writer.write(gpsDailyUpdates+",");
	                writer.write(status+",");
	                writer.write(sensitivity+",");
	            	writer.write(alertDelay+",");
	            	writer.write(allowAccess+",");
	            	writer.write(bSensitivity);
	                /* ensure that everything is
	                 * really written out and close */
	                writer.flush();
	                writer.close();
	                
            	 
            	 }catch(IOException ioe){
            		 Log.e(TAG,"ERROR trying to create file");
 			        ioe.printStackTrace();
            	 }
            	 
             }
			       
		               
					
	}

	public void getPreferences() {
		File file = MAIN_ACTIVITY.getFileStreamPath("preferences.txt");
		if(file.exists())
		{
			try {		prefs.clear();			
				
						BufferedReader reader = new BufferedReader(new FileReader(file));
						for (String line; (line = reader.readLine()) != null;) {
							String[] settings=line.split(",");	
							for(int x=0;x<settings.length;x++){
								prefs.add(settings[x]);
							Log.i(TAG+"Preferences","\n"+settings[x]);
						}
		                }		
							Log.i(TAG,"gettings preferences");
							gpsTracking=prefs.get(0);
			                gpsAccuracy=prefs.get(1);
			                gpsDailyUpdates=prefs.get(2);
			                status=prefs.get(3);
			                sensitivity=prefs.get(4);
			            	alertDelay=prefs.get(5);
			            	allowAccess=prefs.get(6);
			            	bSensitivity=prefs.get(7);
					} catch (IOException ioe) {
						Log.e(TAG, "ERROR trying to RE-access file");
						ioe.printStackTrace();
					}
				
		}else
		{
			Log.i(TAG,"FILE DOESN'T EXIST");
			 gpsTracking = "on";
			gpsAccuracy = "city";
			gpsDailyUpdates = "24x";
			 status = "on";
			sensitivity = "medium";
			 alertDelay = "1min";
			allowAccess = "yes";
			 bSensitivity = "high";
			Log.i(TAG,"Loaded default preferences");
		}
	}
				
}
